package com.example.myapplication.kotlin

import android.R

// 编译时常量, 在编译阶段就会被替换, 因此可以提高性能
const val url = "http://www.itheima.com"
fun main() {
    println("hello world")
    println("~~~~~~~~~变量和常量~~~~~~~~")
    //常 量
    val pi = 3.1415926
    //变量
    var food = "隆江猪脚饭"
    println(food + pi)
    println("~~~~~~~~~函数~~~~~~~~")
    printPlus()
    printPlus2("456")
    printPuse3(1, 2, 3)
    println("~~~~~~~~~数组~~~~~~~~")
    //映射
    val nums = arrayOf(1, 2, 3, 4, 5, 6)
    val nums2 = nums.map { it -> it * 2 }
    println(nums2)
    val nums3 = nums.map { it * 2 }
    //筛选 大于4的
    val nums4 = nums.filter { it > 4 }
    println("~~~~~~~~~循环~~~~~~~~")
    for (num in nums) {
        println(num)
    }
    //1-10 循环10次 依次输出 数字
    for (i in 1..10) {
        println(i)
    }
    //1-10 循环9次 取不到10 依次输出 数字
    for (i in 1 until 10) {
        println(i)
    }
    //1-10 循环5次 步长为2
    for (i in 1..10 step 2) {
        println(i)
    }
    //1-10 循环10次 步长为2 倒序
    for (i in 10 downTo 1 step 2) {
        println(i)
    }
    println("~~~~~~~~~类~~~~~~~~")
    class dog {
        var name = "旺财"
        var age = 5
        fun eat() {
            println("旺财吃东西")
        }
    }

    val d = dog()
    d.eat()

    // 显式构造函数
    class person constructor(name: String, age: Int) {
        init {
            println("name:$name,age:$age")
            //require 断言 抛出异常 不满足条件时触发
            require(age > 0) { "年龄不能小于0" }
        }
    }

    val p = person("小王", 18)

    // 2. 隐式主构造函数
    class Person2(var name: String, var age: Int) {
        // 非法调用会抛出 IllegalArgumentException
        init {
            // 初始化验证
            require(name.isNotEmpty()) { "姓名不能为空" }
            require(age >= 0) { "年龄必须大于0" }
        }

        fun intro() {
            println("name: $name, age: $age")
        }
    }

    /**
     * 次构造函数
     */
    class Person3 {
        var name: String = ""
        var age: Int = 0

        constructor(name: String, age: Int) {
            this.name = name
            this.age = age
        }

        fun intro() {
            println("姓名: $name, 年龄: $age")
        }
    }

    //多个次构造函数
    class Person4(name: String) {
        var age: Int = 0
        var score: Int = 0

        constructor(name: String, age: Int) : this(name) {
            this.age = age
        }

        constructor(name: String, age: Int, score: Int) : this(name, age) {
            this.score = score
        }
    }

    val p4 = Person4("小王", 18, 100)
    println("~~~~~~~~~~~~~~单例类~~~~~~~~~~~~~~~~~")

    //整个应用生命周期只存在一个实例
    //提供全局访问点（通过类名直接访问）
    //线程安全（Kotlin 自动保证）
    //使用 object 关键字声明
    val Singleton1 = Singleton
    val Singleton2 = Singleton
    println("Singleton1==Singleton2:" + (Singleton1 == Singleton2))
    Singleton1.sayHello()
    Singleton.sayHello()//可以通过类名直接访问
}

fun printPlus() {
    println("123")
}

fun printPlus2(s: String) {
    println(s)
}

fun printPuse3(a: Int, b: Int, c: Int): Int {
    return a + b + c
}

object Singleton {
    var name = "小王"
    var age = 18
    fun sayHello() {
        println("hello")
    }
}